c#连接基恩士SR-700扫码枪,支持多枪扫描,用了一些界面UI美化插件,如果没有插件替换成自己的ui即可 private void kryptonButton1_Click(object sender, EventArgs e)
{
if (!m_searcher.IsSearching)
{
removeUnusedReaders();
//开始搜索
m_searcher.Start((res) => {
BeginInvoke(new delegateSearchResult(appendSearchResult), res);
});
searchUIControl(true);
}
}
private void appendSearchResult(ReaderSearchResult res)
{
if (res.IpAddress == "")
{
searchUIControl(false);
return;
}
string key = getKeyFromSearchResult(res);
foreach (string oldkey in kryptonCheckedListBox1.Items)
{
if (key == oldkey) return;
}
kryptonCheckedListBox1.Items.Add(key);
kryptonLabel2.Text = kryptonCheckedListBox1.Items.Count " 个";
}
private string getKeyFromSearchResult(ReaderSearchResult res)
{
return res.IpAddress "/" res.ReaderModel "/" res.ReaderName;
}
private void removeUnusedReaders()
{
for (int i = kryptonCheckedListBox1.Items.Count - 1; i >= 0; i--)
{
String key = kryptonCheckedListBox1.Items[i].ToString();
if (!kryptonCheckedListBox1.GetItemChecked(i))
{
kryptonCheckedListBox1.Items.Remove(key);
}
if (kryptonCheckedListBox1.Items.Count == 0) break;
}
}
private void searchUIControl(bool enable)
{
if (enable)
{
kryptonButton1.Enabled = false;
kryptonButton2.Enabled = false;
kryptonButton3.Enabled = false;
kryptonButton4.Enabled = false;
kryptonCheckedListBox1.Enabled = false;
}
else
{
kryptonButton1.Enabled = true;
kryptonButton2.Enabled = true;
kryptonButton3.Enabled = true;
kryptonButton4.Enabled = true;
kryptonCheckedListBox1.Enabled = true;
}
}
private void kryptonButton4_Click(object sender, EventArgs e)
{
for (int i = 0; i < kryptonCheckedListBox1.Items.Count; i )
{
kryptonCheckedListBox1.SetItemChecked(i, true);//全选
if (kryptonCheckedListBox1.GetItemChecked(i))
{
// kryptonCheckedListBox1.SetSelected(i, true);
string key = kryptonCheckedListBox1.Items[i].ToString();
resisterReaders(key);
}
}
}
private bool resisterReaders(string key)
{
if (m_resisterdReaders.ContainsKey(key)) return false;
ReaderSearchResult result = getSearchResultFromKey(key);
m_resisterdReaders.Add(key, new ReaderAccessor(result.IpAddress));
Console.WriteLine("IpAddress:{0}", result.IpAddress);
return true;
}
private bool removeReaders(string key)
{
if (m_resisterdReaders.ContainsKey(key))
{
m_resisterdReaders[key].Dispose();
m_resisterdReaders.Remove(key);
return true;
}
return false;
}
private ReaderSearchResult getSearchResultFromKey(string key)
{
String[] readerInfo = key.Split('/');
if (readerInfo.Length == 3)
{
return new ReaderSearchResult(readerInfo[1], readerInfo[2], readerInfo[0]);
}
return new ReaderSearchResult();
}
private delegate void delegateUserControl(string str,string IP);
private void kryptonButton2_Click(object sender, EventArgs e)
{
getCheckedListBoxItems();//获取选中的ip
if (kryptonCheckedListBox1.Items !=null) {
foreach (ReaderAccessor reader in m_resisterdReaders.Values)
{
Console.WriteLine("IPAddress:{0}", reader.IpAddress);
reader.Connect((data) =>
{
BeginInvoke(new delegateUserControl(ReceivedDataWrite), Encoding.ASCII.GetString(data),reader.IpAddress);
});
}
kryptonButton2.Enabled = false;
kryptonButton3.Enabled = true;
}
}
评论